/** @file    FormLogViewer.cpp
 *  @author  Alessandro Polo
 *  @version $Id: FormLogViewer.cpp 2253 2010-03-29 11:58:04Z alex $
 *  @brief
 * File containing methods for the wosh::gui::FormLogViewer class.
 * The header for this class can be found in FormLogViewer.h, check that file
 * for class description.
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/
 
 #include "FormLogViewer.h"
 #include "ui_FormLogViewer.h"

// #include <core/ObjectAllocator.h>
 #include <QFontDialog>
 #include <QColorDialog>
 #include <iostream>
 #include <streambuf>


using namespace std;
namespace wosh {
 namespace gui {

// WOSH_REGISTER(wosh::gui::FormLogViewer, "wosh::gui::IWidgetGeneric", 1.0, _static_FormLogViewer )

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

FormLogViewer::FormLogViewer(QWidget *parent) : QWidget(parent)
 {
	this->m_ui = new Ui::FormLogViewer();
	this->m_ui->setupUi(this);

	this->cHandler = NULL;

	mapToDefaultHandler(true);

	connect(this->m_ui->btnFontChange, SIGNAL(clicked()), this, SLOT(btnFontChange_clicked()));
	connect(this->m_ui->btnBackgroundChange, SIGNAL(clicked()), this, SLOT(btnBackgroundChange_clicked()));

 }

FormLogViewer::~FormLogViewer() {
	mapToDefaultHandler(false);

	delete this->m_ui;
 }

void FormLogViewer::changeEvent(QEvent *e) {
	QWidget::changeEvent(e);
	if ( e->type() == QEvent::LanguageChange )
		this->m_ui->retranslateUi(this);
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void FormLogViewer::mapToDefaultHandler( bool enable )
 {
	if ( enable ) {
		if ( this->cHandler != NULL )
			return;
		this->cHandler = new LogHandlerQRenderer();
		this->cHandler->setColoured(true);
		this->cHandler->open();
		connect(this->cHandler, SIGNAL(appendLine(const QString&)), this->m_ui->txtConsole, SLOT(insertHtml(const QString&)), Qt::QueuedConnection);
		wosh::DefaultSharedLogHandler::getInstance()->addHandler(this->cHandler);             // insertPlainText
	}
	else {
		if ( this->cHandler == NULL )
			return;
		wosh::DefaultSharedLogHandler::getInstance()->deleteHandler(this->cHandler);
		disconnect(this->cHandler, SIGNAL(appendLine(const QString&)), this->m_ui->txtConsole, SLOT(insertHtml(const QString&)));
		this->cHandler->close();
		delete this->cHandler; this->cHandler = NULL;
	 }
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void FormLogViewer::btnFontChange_clicked() {
	bool ok;
	QFont font = QFontDialog::getFont( &ok, this->m_ui->txtConsole->font(), this);
	if ( !ok )
		return;
	this->m_ui->txtConsole->setFont(font);
 }

void FormLogViewer::btnBackgroundChange_clicked() {
	QColor color = QColorDialog::getColor(Qt::green, this);
	if ( !color.isValid() )
		return;
	QPalette pal = this->m_ui->txtConsole->palette();
	pal.setColor(QPalette::Background, color);
	pal.setColor(QPalette::Window, color);
	this->m_ui->txtConsole->setPalette(pal);
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 }; // namespace gui
}; // namespace wosh
